<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { getStockData } from '@/API/api.js'
export default {
  name: 'Stock',
  data() {
    return {
      myChart: null,
      allData: null,
      currentIndex: 0,
      timer: null
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdater)
    this.screenAdater()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdater)
    clearInterval(this.timer)
  },
  methods: {
    // 初始化 echarts 对象
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.stock_ref, 'chalk')
      const initOption = {
        title: {
          text: '▎库存与销量分析',
          top: 20,
          left: 20
        }
      }
      this.myChart.setOption(initOption)
      this.myChart.on('mouseover', () => {
        clearInterval(this.timer)
      })
      this.myChart.on('mouseout', () => {
        this.startInterval()
      })
    },
    // 获取数据
    async getData() {
      const { data: res } = await getStockData()
      // console.log(res)
      this.allData = res
      this.updataChart()
      this.startInterval()
    },
    // 处理数据，更新图表
    updataChart() {
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start, end)
      const seriesData = showData.map((item, index) => {
        return {
          type: 'pie',
          center: centerArr[index],
          data: [
            {
              name: item.name + '\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ],
          hoverAnimation: false,
          labelLine: {
            show: false
          },
          label: {
            position: 'center',
            color: colorArr[index][0]
          }
        }
      })
      const dataOption = {
        series: seriesData
      }
      this.myChart.setOption(dataOption)
    },
    // 分辨率适配
    screenAdater() {
      const titleFontSize = (this.$refs.stock_ref.offsetWidth / 100) * 3.6
      const insetRadius = titleFontSize * 2
      const outRadius = insetRadius * 1.125
      const adaterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outRadius, insetRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outRadius, insetRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outRadius, insetRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outRadius, insetRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outRadius, insetRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          }
        ]
      }
      this.myChart.setOption(adaterOption)
      this.myChart.resize()
    },
    startInterval() {
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.timer = setInterval(() => {
        this.currentIndex++
        if (this.currentIndex > 1) {
          this.currentIndex = 0
        }
        this.updataChart()
      }, 5000)
    }
  }
}
</script>

<style lang="less" scoped></style>
